package com.blb.zy_community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zy_community.entity.SysRole;
import com.blb.zy_community.entity.SysRoleDept;
import com.blb.zy_community.entity.SysRoleMenu;
import com.blb.zy_community.mapper.SysRoleDeptMapper;
import com.blb.zy_community.mapper.SysRoleMapper;
import com.blb.zy_community.mapper.SysRoleMenuMapper;
import com.blb.zy_community.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author 二组
 * @since 2023-06-18
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    private SysRoleDeptMapper sysRoleDeptMapper;
    @Override
    public Page<SysRole> selectRolePage(Page<SysRole> page,String roleName,String roleKey,String roleStatus,String startTime,String endTime) {
        return sysRoleMapper.selectRolePage(page,roleName,roleKey,roleStatus,startTime,endTime);
    }

    @Override
    public void deleteRoles(Long[] roleIds) {
        sysRoleMapper.deleteRoles(roleIds);
    }

    @Transactional
    @Override
    public void addRoleAndMenu(SysRole role, List menuIds) throws Exception {
        if(StringUtils.isEmpty(menuIds)){
            throw new Exception("角色为空");
        }
        SysRole roleName = sysRoleMapper.checkName(role.getRoleName());
        if(!StringUtils.isEmpty(roleName)){
            throw new Exception("用户名重复");
        }
        sysRoleMapper.insert(role);
        SysRole sysRole = sysRoleMapper.selectOne(new QueryWrapper<SysRole>().lambda().eq(SysRole::getRoleName, role.getRoleName()));
        Long roleId = sysRole.getRoleId();
        for (Object menuId : menuIds) {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId((Long) menuId);
            sysRoleMenuMapper.insert(roleMenu);
        }
    }

    @Override
    public void addRoleAndDept(SysRole role, List deptIds) {
        if(StringUtils.isEmpty(deptIds)){
            throw new RuntimeException("权限为空");
        }
        SysRole sysRole = sysRoleMapper.selectOne(new QueryWrapper<SysRole>().lambda().eq(SysRole::getRoleName, role.getRoleName()));
        sysRoleDeptMapper.delete(new QueryWrapper<SysRoleDept>().lambda().eq(SysRoleDept::getRoleId,role.getRoleId()));
        Long roleId = sysRole.getRoleId();
        for (Object deptId : deptIds) {
            SysRoleDept sysRoleDept = new SysRoleDept();
            sysRoleDept.setRoleId(roleId);
            sysRoleDept.setDeptId((Long) deptId);
            sysRoleDeptMapper.insert(sysRoleDept);
        }
    }


    @Override
    public void updateDelFlagId(Long roleId) {
        sysRoleMapper.updateDelFlagId(roleId);
    }

    @Override
    public void updateStatus(Long statusId, Long roleId) {
        sysRoleMapper.updateStatus(statusId,roleId);
    }

    @Override
    public void updateRoleAndMenu(SysRole role, List menuIds) {
        if(StringUtils.isEmpty(menuIds)){
            throw new RuntimeException("角色为空");
        }
        sysRoleMenuMapper.delete(new QueryWrapper<SysRoleMenu>().lambda().eq(SysRoleMenu::getRoleId,role.getRoleId()));
        for (Object menuId : menuIds) {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setRoleId(role.getRoleId());
            roleMenu.setMenuId((Long) menuId);
            sysRoleMenuMapper.insert(roleMenu);
        }
        sysRoleMapper.updateById(role);
    }

    @Override
    public void getRoleName(Long id) {
        sysRoleMapper.selectRoleName(id);
    }
}
